package com.breakfast.fun.ui.home.gallery;

import com.breakfast.fun.data.entity.home.product.ProductComment;
import com.breakfast.fun.util.Api;
import com.breakfast.fun.util.T;

import java.util.Map;

import rx.Subscriber;
import rx.android.schedulers.AndroidSchedulers;
import rx.schedulers.Schedulers;

/**
 * Created by yukai on 16/9/29.
 */

public class ProductCommentPresenter extends ProductCommentContract.Presenter {
    @Override
    public void getComment(String productId, int begin) {
        Map<String, String> map = Api.getInstance().getBaseParams();

        map.put("action", "comment");
        map.put("id", productId);
        map.put("begin", begin + "");
        map.put("end", "10");

        showLoadingDialog(true);

        rxManager.add(model.getProductCommentModel(map)
                .subscribeOn(Schedulers.computation())
                .observeOn(AndroidSchedulers.mainThread())
                .unsubscribeOn(Schedulers.io())
                .subscribe(new Subscriber<ProductComment>() {
                    @Override
                    public void onCompleted() {
                    }

                    @Override
                    public void onError(Throwable e) {
                        e.printStackTrace();
                        hideLoadingDialog();
                        T.show("获取数据失败");
                    }

                    @Override
                    public void onNext(ProductComment productComment) {
                        hideLoadingDialog();
                        if (productComment.isSuccess()) {
                            view.showComment(productComment);
                        } else {
                            T.show(productComment.getMsg());
                        }
                    }
                }));
    }

    @Override
    public void loadComment(String productId, int begin) {
        Map<String, String> map = Api.getInstance().getBaseParams();

        map.put("action", "comment");
        map.put("id", productId);
        map.put("begin", begin + "");
        map.put("end", "10");


        rxManager.add(model.getProductCommentModel(map)
                .subscribeOn(Schedulers.computation())
                .observeOn(AndroidSchedulers.mainThread())
                .unsubscribeOn(Schedulers.io())
                .subscribe(new Subscriber<ProductComment>() {
                    @Override
                    public void onCompleted() {
                    }

                    @Override
                    public void onError(Throwable e) {
                        T.show("获取数据失败");
                    }

                    @Override
                    public void onNext(ProductComment productComment) {
                        if (productComment.isSuccess()) {
                            view.loadComment(productComment);
                        } else {
                            T.show(productComment.getMsg());
                        }
                    }
                }));
    }

    @Override
    public void onStart() {
        getComment(view.getProductId(), 0);
    }
}
